<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <h2>隐式转换</h2>
    <ul>
        <li>布尔类型和数值类型进行数学运算，布尔类型会先转为数值类型。</li>
        <li>true=1；false=0</li>
        <li>字符串如鞥与数值类型进行数学运算（除了加法），也会先隐式转为数值类型。</li>
    </ul>
    <h2>显示转换</h2>
    <ul>
        <li>js提供了一些函数，主要用于将字符串转为数值。</li>
        <li>parseInt(str)：将传入的字符串转为整数（只允许传入数值开头的字符串）。</li>
        <li>parseFloat(str)：将传入的字符串转为浮点数（只允许传入数值开头的字符串）。</li>
        <li>Number(var)：将传入的参数转为数值（可以串入任意基础数据类型）。</li>
    </ul>
    <script>
        let boo1 = true;
        let n = boo1 + 5;
        console.log("n = ",n);

        let str = "123.0";
        n = 245;

        let r = (str * 1) + n;
        console.log("r = ",r); // 368

        str = "123.99";
        console.log("parseInt(str) = ",parseInt(str)); // 123

        str = "123我";
        console.log("parseInt(str) = ",parseInt(str)); // 123

        str = "123.99";
        console.log("parseFloat(str) = ",parseFloat(str)); // 123.99

        str = "123.99我";
        console.log("parseFloat(str) = ",parseFloat(str)); // 123.99

        // Number()函数
        str = "123.99";
        console.log("Number(str) = ",Number(str)); //123.99

        console.log("Number(true) = ",Number(true)); // 1
        console.log("Number(false) = ",Number(false)); // 0
        console.log("Number(null) = ",Number(null)); // 0
        console.log("Number(undefined) = ",Number(undefined)); //NaN
        console.log("Number('123.99我') = ",Number('123.99我')); //NaN
    </script>
</body>

</html>